<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Service\KehuService as Kehu;

class KehuController extends Controller
{
    //客户管理

    public function sel()
    {
        $res = Kehu::sel();
        return $this->toJson(0, '', $res);
    }

    //列表
    public function index(Request $request)
    {
        $pas['area_id'] = $request->post('area_id', 0);
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 20);
        $res = Kehu::index($pas, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //详情
    public function one(Request $request)
    {
        $id = $request->post('id', 0);
        $res = Kehu::one($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //录入客户
    public function add(Request $request)
    {
        $pas = $request->all();
        $pas = $pas['txt'];
        $pas = explode("\n", $pas);
        $arys = [];
        foreach ($pas as $k => $v) {
            $v = preg_replace('/ +/', '	', $v);
            $vs = explode('	', $v);
//            ddd($vs);
            $arys[$k] = [
                'cname' => trim($vs[0]),
                'cstatus' => trim($vs[1]),
                'xydm' => trim($vs[2]),
                'regist_no' => trim($vs[3]),
                'province' => trim($vs[4]),
                'city' => trim($vs[5]),
                'area' => trim($vs[6]),
                'boss' => trim($vs[7]),
                'zijin_regist' => trim($vs[8]),
                'zijin_real' => trim($vs[9]),
                'date_start' => trim($vs[10]),
                'date_check' => trim($vs[11]),
                'old_name' => trim($vs[12]),
                'hangye' => trim($vs[14]),
                'qiye_type' => trim($vs[15]),
                'zzjgdm' => trim($vs[16]),
                'shebao' => trim($vs[17]),
                'range' => trim($vs[18]),
                'dizhi_gs' => trim($vs[19]),
                'dizhi_new' => trim($vs[20]),
                'email_gs' => trim($vs[21]),
                'gudong' => trim($vs[23]),
                'tel' => trim($vs[24]),
                'tel2' => trim($vs[25]),
                'addtime' => toDate(),
            ];
        }
//        ddd($arys);
        Kehu::add($arys);
        return $this->toJson();
    }

    //定位城市
    public function toarea(){
        $res = Kehu::toarea();
        if (!$res) {
            ddd('err'); exit;
        }
        return redirect('/api/kehu/toarea');
    }
}
